home *** CD-ROM | disk | FTP | other *** search
/ Gekikoh Dennoh Club 1 / Gekikoh Dennoh Club Vol. 1 (Japan).7z / Gekikoh Dennoh Club Vol. 1 (Japan) (Track 1).bin / kowin / archive / net / chatwins.lzh / hist.c < prev    next >
C/C++ Source or Header  |  1993-05-09  |  1KB  |  83 lines

  1. /*    Copyright 1993 H.Ogasawara (COR.)    */
  2.  
  3. #include    <corlib.h>
  4.  
  5. #define        MAXCHAR        160
  6.  
  7. typedef struct _hist {
  8.         char        *line;
  9.         struct _hist    *next;
  10.     } T_HIST;
  11.  
  12. char    savebuf[MAXCHAR+2];
  13. T_HIST    curline= { savebuf, NULL };
  14. T_HIST    *lasthp= NULL;
  15.  
  16.  
  17. static T_HIST *
  18. prevent( pp )
  19. T_HIST    *pp;
  20. {
  21.     T_HIST    *hp, *prevp= NULL;
  22.     for( hp= lasthp ; pp != hp && hp ; prevp= hp, hp= hp->next );
  23.     return    prevp;
  24. }
  25.  
  26. addhist( msg )
  27. char    *msg;
  28. {
  29.     int    i;
  30.     char    *ptr;
  31.     T_HIST    *hp;
  32.     if( *msg == '\0' || *msg == '\r' || *msg == '\n' )
  33.         return    TRUE;
  34.     if( lasthp && !strcmp( msg, lasthp->line ) )
  35.         return    TRUE;
  36.     if( lasthp == &curline )
  37.         lasthp= lasthp->next;
  38.     for(;;){
  39.         if( hp= (void*)malloc( sizeof(T_HIST)+strlen(msg)+2 ) ){
  40.             ptr= ((char*)hp)+sizeof(T_HIST);
  41.             strcpy( ptr, msg );
  42.             hp->line= ptr;
  43.             hp->next= lasthp;
  44.             lasthp= hp;
  45.             return    TRUE;
  46.         }
  47.         if( lasthp ){
  48.             for( hp= lasthp ; hp->next ; hp= hp->next );
  49.             if( hp= prevent( hp ) ){
  50.                 free( hp->next );
  51.                 hp->next= NULL;
  52.                 continue;
  53.             }
  54.         }
  55.         return    FALSE;
  56.     }
  57. }
  58.  
  59.  
  60. getnexthist( ibuf, histflag, dd )
  61. char    *ibuf;
  62. {
  63.     static T_HIST    *hp;
  64.     if( !histflag ){
  65.         if( lasthp != &curline )
  66.             curline.next= lasthp;
  67.         hp= lasthp= &curline;
  68.         strcpy( savebuf, ibuf );
  69.     }
  70.     if( dd ){
  71.         if( hp->next ){
  72.             hp= hp->next;
  73.             strcpy( ibuf, hp->line );
  74.         }
  75.     }else{
  76.         if( prevent( hp ) ){
  77.             hp= prevent( hp );
  78.             strcpy( ibuf, hp->line );
  79.         }
  80.     }
  81. }
  82.  
  83.